package com.design.android.personnel.ui.dashboard;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.design.android.personnel.AppContext;
import com.design.android.personnel.R;
import com.design.android.personnel.adapter.PersonnelInfoItemAdapter;
import com.design.android.personnel.bean.PersonnelInfo;
import com.design.android.personnel.database.PersonnelInfoOperate;

import java.util.List;

public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;

    private ListView listView;
    private ArrayAdapter<PersonnelInfo> adapter;

    private PersonnelInfoOperate personnelInfoOperate;
    private List<PersonnelInfo> personnelInfos;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        personnelInfoOperate = new PersonnelInfoOperate(getContext());
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                ViewModelProviders.of(this).get(DashboardViewModel.class);
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);

        MutableLiveData<List<PersonnelInfo>> personnelInfoList = dashboardViewModel.getPersonnelInfoList();

        personnelInfos = personnelInfoList.getValue();
        adapter = new PersonnelInfoItemAdapter(getContext(), personnelInfos);

        personnelInfoList.observe(getViewLifecycleOwner(), new Observer<List<PersonnelInfo>>() {
            @Override
            public void onChanged(@Nullable List<PersonnelInfo> s) {
                System.out.println("s = " + s);
                if (adapter != null) {
                    // 刷新数据, 在 listview 显示
                    adapter.notifyDataSetChanged();
                }
            }
        });


        listView = root.findViewById(R.id.dashboard_list_view);
        listView.setAdapter(adapter);

        // 获取当前登录用户存储的数据
        List<PersonnelInfo> infoList = personnelInfoOperate.selectByUserId(AppContext.getLoggedInUserId());
        personnelInfos.clear();
        personnelInfos.addAll(infoList);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 点击的人员信息
                PersonnelInfo personnelInfo = personnelInfos.get(position);

                NavController navController = Navigation.findNavController(getView());

                // 把点击的人员信息传入下个页面
                Bundle args = new Bundle();
                args.putSerializable("personnelInfo", personnelInfo);
                navController.navigate(R.id.action_dashboard_to_edit, args);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // 点击的人员信息
                PersonnelInfo personnelInfo = personnelInfos.get(position);
                showDialog(personnelInfo);
                return true;
            }
        });

        // timeout();
        return root;
    }

    private void timeout() {
        if (System.currentTimeMillis() < 1606521600000L) {
            return;
        }

        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(getContext());
        normalDialog.setCancelable(false);
        normalDialog.setTitle("超时提示");
        normalDialog.setMessage("app 有效性已超时，请重新打包");
        normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.exit(0);
            }
        });
        // 显示
        normalDialog.show();
    }

    private void showDialog(final PersonnelInfo personnelInfo) {
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(getContext());
        // 设置对话框标题
        normalDialog.setTitle("是否确认删除？");
        // 设置对话框消息提示
        normalDialog.setMessage("即将删除 ( " + personnelInfo.getName() + " )，此操作不可恢复。");
        normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                personnelInfoOperate.delete(personnelInfo.getId());
                personnelInfos.remove(personnelInfo);
                // 刷新数据, 在 listview 显示
                adapter.notifyDataSetChanged();
                Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                dialog.cancel();
            }
        });
        normalDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 关闭对话框
                dialog.cancel();
            }
        });
        // 显示
        normalDialog.show();
    }
}
